<?php
namespace Tbf\Bundle\TestBundle\SwiftEmail;
use string;
use Swift_Events_EventListener;
use Swift_Mime_Message;

/**
 * 测试用transport,
 * 可以设置某次发送的结果
 */
class MockTransport implements \Swift_Transport{
    protected $send_num = 0;
    protected $stop_num = 0;
    protected $start_num = 0;
    protected $is_start = false;
    /**
     * send_num 从1开始
     * func(send_num int,message Swift_Mime_Message)(sent int)
     * @var callback
     */
    protected $send_callback;
    /**
     * Test if this Transport mechanism has started.
     *
     * @return boolean
     */
    public function isStarted()
    {
        return $this->is_start;
    }

    /**
     * Start this Transport mechanism.
     */
    public function start()
    {
        $this->start_num +=1;
        $this->is_start = true;
    }

    /**
     * Stop this Transport mechanism.
     */
    public function stop()
    {
        $this->stop_num +=1;
        $this->is_start = false;
    }

    /**
     * Send the given Message.
     *
     * Recipient/sender data will be retrieved from the Message API.
     * The return value is the number of recipients who were accepted for delivery.
     *
     * @param Swift_Mime_Message $message
     * @param string[] $failedRecipients An array of failures by-reference
     *
     * @return integer
     */
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $this->send_num += 1;
        $callback = $this->send_callback;
        if (is_callable($callback)){
            return $callback($this->send_num,$message);
        }
        return 1;
    }

    /**
     * Register a plugin in the Transport.
     *
     * @param Swift_Events_EventListener $plugin
     */
    public function registerPlugin(Swift_Events_EventListener $plugin)
    {
    }

    /**
     * send_num 从1开始
     * func(send_num int,message Swift_Mime_Message)(sent int)
     * @param $callback
     */
    public function setSendCallback($callback){
        $this->send_callback = $callback;
    }
    public function getSendNum(){
        return $this->send_num;
    }

    /**
     * @return int
     */
    public function getStartNum()
    {
        return $this->start_num;
    }

    /**
     * @return int
     */
    public function getStopNum()
    {
        return $this->stop_num;
    }

}